Skip to content

cargo_build_script: set the Apple deployment target for cc-rs - #4434

Open
TrapsterDK wants to merge 1 commit into
bazelbuild:mainfrom
TrapsterDK:cargo-build-script-apple-deployment-target
Open

TrapsterDK wants to merge 1 commit into
bazelbuild:mainfrom
TrapsterDK:cargo-build-script-apple-deployment-target

Conversation

@TrapsterDK

@TrapsterDK TrapsterDK commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

#4282 made rustc actions set MACOSX_DEPLOYMENT_TARGET (and the iOS, tvOS, watchOS and visionOS variables) from the deployment target in the cc_toolchain's link args. cargo_build_script still doesn't set them. cc-rs does not read the deployment target from CFLAGS. It reads these environment variables, and when they are unset it falls back to its own default. So a C dependency built by a build script can target a different OS version than the toolchain, even though CFLAGS carries the right -target/-m<os>-version-min=.

Fix

  • cargo_build_script runs apple_deployment_target_env on the cc_toolchain's C compile args and sets the resulting variable for the build script. The helper is the one rustc actions already use. The version comes from the last -target/--target= Apple triple and falls back to -m<os>-version-min=. When both are present the triple wins, as in clang.
  • Precedence follows rustc actions, plus a use_default_shell_env case that only build scripts have:
    1. The toolchain's env and build_script_env override the derived value.
    2. A value set with --action_env=VAR=value is kept, but only when use_default_shell_env is on. With it off, --action_env never reaches the action, so the derived value is still set.
    3. An inherited --action_env=VAR with no value is not visible at analysis time, so the derived value overrides it.
  • Updated the apple_deployment_target_env docstring to cover compile args, and the use_cc_toolchain setting docs to list the *_DEPLOYMENT_TARGET variables. Also removed a stale comment saying cc-rs takes the deployment target from CFLAGS.

Tests

New analysis tests in cargo/tests/cargo_build_script/cc_args_and_env, under apple_deployment_target_test:

  • macOS -target triple, and iOS -mios-version-min=.
  • -target combined with -mmacosx-version-min=, in both orders. The triple wins.
  • A non-Apple triple sets none of the deployment target variables.
  • --action_env=MACOSX_DEPLOYMENT_TARGET=... is kept.
  • With use_default_shell_env = False plus --action_env, the derived value is set.
  • build_script_env overrides the derived value.

bazel test //cargo/tests/cargo_build_script/cc_args_and_env/... //test/unit/apple_deployment_target/...: 51 tests pass. The --action_env tests fail if the default_shell_env check or its use_default_shell_env guard is removed.

Assisted by Claude.

cc-rs reads MACOSX_DEPLOYMENT_TARGET and its per-OS siblings, not CFLAGS.
Derive them from the C compile args, as rustc actions already do from the
link args. As for rustc, a value set through --action_env is kept, and
the toolchain's env and build_script_env still override the derived one.

Assisted-by: Claude
@TrapsterDK
TrapsterDK force-pushed the cargo-build-script-apple-deployment-target branch from 3ace926 to fdfd065 Compare November 18, 2026 12:30

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant